//
//  SYSelectClassViewController.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/3/30.
//  Copyright © 2020年 SanYi. All rights reserved.
//

#import "SYSelectClassViewController.h"
#import "SYSelectClassView.h"


@interface SYSelectClassViewController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *campusView;
@property (weak, nonatomic) IBOutlet UITableView *curTableView;
@property (strong, nonatomic) SYSelectClassView *classView;
@property (strong, nonatomic) NSMutableArray *dataListArr;


@end

@implementation SYSelectClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _classView = [[SYSelectClassView alloc] init];
    [self.campusView addSubview:_classView];
    [_classView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.campusView);
    }];
    _curTableView.tableFooterView = [UIView new];
    
    //点击了分校
    __weak typeof(self) weakSelf = self;
    
    
    
    self.classView.block = ^(NSString * _Nonnull campusId) {

        [weakSelf requestDataListCampusId:campusId];

    };
    
    
    self.dataListArr = [NSMutableArray array];
    
}
//请求分校班级
- (void)requestDataListCampusId:(NSString *)campusId {
    
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestClassByCampusListCampusId:campusId name:@"" success:^(NSMutableArray *listArr) {
        [weakSelf.dataListArr removeAllObjects];
        [weakSelf.dataListArr addObjectsFromArray:listArr];
        [weakSelf.curTableView reloadData];
    } fail:^(NSError *error) {
        
    }];
    
    
}
//请求数据
- (void)requestDataList {
    //请求数据
    [self.classView requestData];
}

#pragma mark - tab
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    NSDictionary *dict = self.dataListArr[indexPath.row];
    cell.textLabel.text = [dict nilObjectForKey:@"className"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dict = self.dataListArr[indexPath.row];
//    if (self.selectBlock) {
//        self.selectBlock([dict nilObjectForKey:@"classId"], [dict nilObjectForKey:@"className"]);
//    }

    if ([self.delegate respondsToSelector:@selector(selectClassViewClassId:className:)]) {
        [self.delegate selectClassViewClassId:[dict nilObjectForKey:@"classId"] className:[dict nilObjectForKey:@"className"]];
    }
    
}



#pragma mark -
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
